
package com.rx.mgt.controller;  

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rx.mgt.util.MD5;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.rx.mgt.domain.AdminUser;
import com.rx.mgt.domain.UserInfoExt;
import com.rx.mgt.service.SysLogService;
import com.rx.mgt.service.UserInfoExtService;
import com.rx.mgt.util.DateUtil;
import com.rx.mgt.util.ExcelUtil;
import com.rx.mgt.util.IpUtil;

/** 
 * ClassName:BatchUploadController 
 * Function :
 * Reason   : 
 * Date     :Feb 4, 2017 3:31:06 PM 
 * @author   luolei 
 * @since    JDK 1.6
 */
@Controller
public class BatchUploadController {
    @Autowired
    UserInfoExtService userInfoExtService;
    @Autowired
    SysLogService sysLogService;
    @ResponseBody
    @RequestMapping(value="/batchRegister.htmlx",  method=RequestMethod.POST )
    public void  batchRegister (HttpServletRequest request , HttpServletResponse response) throws IOException{
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("批量添加用户", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        
        response.setContentType("text/html;charset=utf-8");
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        /*页面控件流*/
        MultipartFile mFile  = multipartRequest.getFile("uploadfile");
        /**获取文件的后缀**/
        String suffix = mFile.getOriginalFilename().substring(mFile.getOriginalFilename().lastIndexOf("."));

        if(! ".xlsx".equals(suffix)){
            response.getWriter().print("{\"errMsg\":\"格式错误\"}");
        }
        try{
            byte[] bb = mFile.getBytes();
            InputStream in = new ByteArrayInputStream(bb);
            Map<String,Object> excelMap = ExcelUtil.ExcelToMap(mFile.getInputStream());
            if(excelMap == null || excelMap.isEmpty() ){
                response.getWriter().print("{\"errMsg\":\"处理失败\"}");
                return;
            }
            List<HashMap<String,String> > userListMap = (List<HashMap<String,String> >)excelMap.get("userList");
            for(HashMap<String,String> map : userListMap){
                UserInfoExt userInfoExt = new UserInfoExt();
                String userName = map.get("user_name");
                if(StringUtils.isBlank(userName)||!userName.contains("@")){
                    continue;
                }
                String domain = userName.substring(userName.indexOf("@")+1);
                if("2".equals(user.getType())&&!user.getDomain().equals(domain)){
                  continue;
                }

                userInfoExt.setUser_name(userName);
                userInfoExt.setAddress(map.get("address"));
                userInfoExt.setCreate_time(DateUtil.nowTimeString());
                userInfoExt.setDomain(domain);
                userInfoExt.setName(map.get("name"));
                userInfoExt.setPassword(map.get("password"));
                userInfoExt.setRemark(map.get("remark"));
                userInfoExt.setSex(map.get("sex"));
                userInfoExt.setStatus(map.get("status"));
                userInfoExt.setTelephone(map.get("telephnoe"));
                userInfoExt.setGjjAccount(map.get("gjjAccount"));
                userInfoExt.setMax_mail_box_size(Long.valueOf(org.springframework.util.StringUtils.isEmpty(map.get("max_mail_box_size"))?"0":map.get("max_mail_box_size")));
                userInfoExt.setMax_message_size(Long.valueOf(org.springframework.util.StringUtils.isEmpty(map.get("max_message_size"))?"0":map.get("max_message_size")));
                userInfoExtService.batchRegist(userInfoExt);
            }
            response.getWriter().print("{\"errMsg\":\"处理完成\",\"code\":\"1\"}");
        }catch(Exception e){
            e.printStackTrace();
            response.getWriter().print("{\"errMsg\":\"处理失败\"}");
        }
    }


}
  